package com.wjttest.ripple;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import static ohos.agp.render.Paint.Style.FILLANDSTROKE_STYLE;
import static ohos.agp.render.Paint.Style.STROKE_STYLE;
import static ohos.multimodalinput.event.TouchEvent.POINT_MOVE;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_DOWN;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_UP;

/**
 * 水波纹
 *
 * @since 2021-07-13
 */
public class RippleView extends DependentLayout implements Component.DrawTask, Component.TouchEventListener {
    private float radiusMax;
    private Paint paint;
    private float ripplePose;
    private float downX;
    private boolean animationRunning;
    private float touchX;
    private AnimatorValue rippleAnimation;
    public RippleView(Context context) {
        this(context,null,null);
    }

    public RippleView(Context context, AttrSet attrSet) {
        this(context,attrSet,null);
    }

    public RippleView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        this.setTouchEventListener(this);
        inint();
        this.radiusMax = (float) Math.sqrt(this.getWidth() * this.getWidth() + this.getHeight() * this.getHeight());
        System.out.println("radiusMax！！！！=="+radiusMax);
        this.addDrawTask(this);
    }

    private void inint() {
        this.animationRunning = false;
        this.paint = new Paint();
        this.paint.setAntiAlias(true);
//        this.paint.setStyle(STROKE_STYLE);//描边
        this.paint.setStyle(FILLANDSTROKE_STYLE);//描边
        this.paint.setColor(new Color(Color.getIntColor("#0000FF")));
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        float touchX = (float) this.getWidth() / 2;
        float touchY = (float) this.getHeight() / 2;
        this.paint.setAlpha(1 - 1* this.ripplePose);//透明都也是从0到1  因为ripplePose 是从0-1变换
        this.radiusMax = (float) Math.sqrt(this.getWidth() * this.getWidth() + this.getHeight() * this.getHeight());   // Math.sqrt((a * a) + (b * b)) 计算弧度
        this.radiusMax = component.getWidth()/2;
        float radiusMax2 = component.getWidth()/4;
        System.out.println("radiusMax=="+radiusMax);
        float var3 = this.radiusMax * this.ripplePose - this.radiusMax * (this.ripplePose - 0.4F / 0.6F);
        canvas.drawCircle(touchX, touchY,  this.radiusMax * this.ripplePose +radiusMax2, this.paint);
//        canvas.drawCircle(touchX, touchY,  this.radiusMax * this.ripplePose - var3 / 2, this.paint);

    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        System.out.println("anxia");
        if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == PRIMARY_POINT_DOWN) { // 一个手指按下
            System.out.println("anxia");
            this.downX = this.getTouchX(touchEvent, 0);
            this.createAnimation(this.getTouchX(touchEvent, 0), this.getTouchY(touchEvent, 0));
        } else if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == POINT_MOVE) { // 一个手指移动


        } else if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == PRIMARY_POINT_UP) { //一个手指抬起
            this.invalidate();
        }
        return false;
    }

    /**
     * TouchEvent
     *
     * @param touchEvent TouchEvent
     * @param var2 int
     * @return float
     */
    private float getTouchX(TouchEvent touchEvent, int var2) {
        float var3 = 0.0F;
        if (touchEvent.getPointerCount() > var2) {
            int[] locationOnScreen = this.getLocationOnScreen();
            if (locationOnScreen != null && locationOnScreen.length == 2) {
                var3 = touchEvent.getPointerScreenPosition(var2).getX() - (float) locationOnScreen[0];
            } else {
                var3 = touchEvent.getPointerPosition(var2).getX();
            }
        }

        return var3;
    }

    /**
     *
     *
     * @param var1 float
     * @param var2 float
     */
    private void createAnimation(float var1, float var2) {
        if (this.isEnabled() && !this.animationRunning) {
            if (this.rippleAnimation == null) {
                this.rippleAnimation = new AnimatorValue();
                this.rippleAnimation.setDuration((500l));
                this.rippleAnimation.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                    @Override
                    public void onUpdate(AnimatorValue animatorValue, float v) {
                        RippleView.this.ripplePose = v;
                        RippleView.this.invalidate();
                    }
                });
                this.rippleAnimation.setStateChangedListener(new Animator.StateChangedListener() {
                    public void onStart(Animator var1) {
                    }

                    public void onStop(Animator var1) {
//                        if (downPose) {
//                            isYinYing = true;
//                            RippleView.this.invalidate();
//                        }
                    }

                    public void onCancel(Animator var1) {
                    }

                    public void onEnd(Animator var1) {
                        RippleView.this.animationRunning = false;
                    }

                    public void onPause(Animator var1) {
                    }

                    public void onResume(Animator var1) {
                    }
                });
            }

            if (this.rippleAnimation.isRunning()) {
                return;
            }

            this.animationRunning = true;
            this.touchX = var1;
            this.rippleAnimation.start();
        }
    }


    private float getTouchY(TouchEvent var1, int var2) {
        float var3 = 0.0F;
        if (var1.getPointerCount() > var2) {
            int[] var4 = this.getLocationOnScreen();
            if (var4 != null && var4.length == 2) {
                var3 = var1.getPointerScreenPosition(var2).getY() - (float) var4[1];
            } else {
                var3 = var1.getPointerPosition(var2).getY();
            }
        }

        return var3;
    }
}